package cn.tedu.crm.engineeringManagement.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.tedu.crm.base.response.JsonResult;
import cn.tedu.crm.customer.mapper.CustomerFollowUpMapper;
import cn.tedu.crm.customer.pojo.entity.CustomerFollowUp;
import cn.tedu.crm.engineeringManagement.mapper.EngineeringManagementMapper;
import cn.tedu.crm.engineeringManagement.pojo.dto.EngineeringManagementControllerQuery;
import cn.tedu.crm.engineeringManagement.pojo.dto.EngineeringManagementSaveParam;
import cn.tedu.crm.engineeringManagement.pojo.entity.EngineeringManagement;
import cn.tedu.crm.engineeringManagement.pojo.vo.EngineeringManagementVO;
import cn.tedu.crm.engineeringManagement.service.EngineeringManagementService;
import cn.tedu.crm.operatelog.loggable.Loggable;
import cn.tedu.crm.operatelog.loggable.OperationModule;
import cn.tedu.crm.operatelog.loggable.OperationType;
import cn.tedu.crm.orders.mapper.OrdersMapper;
import cn.tedu.crm.orders.pojo.entity.Orders;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class EngineeringManagementServiceImpl implements EngineeringManagementService{

    @Autowired
    private EngineeringManagementMapper engineeringManagementMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private CustomerFollowUpMapper customerFollowUpMapper;


    @Override
    public List<EngineeringManagementVO> selectEngineeringManagementServiceByCustomerId(EngineeringManagementControllerQuery engineeringManagementControllerQuery) {

        return engineeringManagementMapper.selectEngineeringManagementServiceByCustomerId(engineeringManagementControllerQuery);
    }

    @Override
    @Loggable(module = OperationModule.DEPOSIT_MANAGEMENT,type = OperationType.INSERT)
    public JsonResult insertEngineeringManagement(EngineeringManagementSaveParam engineeringManagementSaveParam) {
        log.debug("插入工程订单表业务层:{}", engineeringManagementSaveParam);
        EngineeringManagement eng = new EngineeringManagement();
        eng.setCollectionTime(new Date());
        eng.setFollowUpResults("20");
        eng.setEnteredBy(StpUtil.getSession().getString("username"));
        BeanUtils.copyProperties(engineeringManagementSaveParam,eng);
        engineeringManagementMapper.insert(eng);
        if (engineeringManagementSaveParam.getOrdersId() != null) {
            Orders orders = new Orders();
            orders.setId(engineeringManagementSaveParam.getOrdersId());
            orders.setDesignerProgress(10);
            orders.setUpdateTime(new Date());
            orders.setUpdateUser(StpUtil.getLoginIdAsLong());
            orders.setCost(new BigDecimal(engineeringManagementSaveParam.getDepositAmount()));
            ordersMapper.update(orders);
            CustomerFollowUp customerFollowUp = new CustomerFollowUp();
            customerFollowUp.setCustomerId(engineeringManagementSaveParam.getCustomerId());
            customerFollowUp.setStatus("50");
            customerFollowUp.setUpdateTime(new Date());
            customerFollowUpMapper.updateByIdQ(customerFollowUp);
        }
        return JsonResult.ok();
    }
}




